package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.astrob.view.BaseViewHolder;
import com.nhc.model.CoordParmFileHandle;
import com.nhc.model.LambertParmFileHandle;
import com.nhc.model.TransParaSevenFileHandle;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.model.Entry;
import zuo.biao.library.ui.BottomMenuWindow;
import zuo.biao.library.ui.ItemDialog;
import zuo.biao.library.ui.PageScroller;

public class CoordSetActivity extends BaseActivity implements ItemDialog.OnDialogItemClickListener {
    private final String TAG = "CoordSetActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, CoordSetActivity.class);
    }
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coordset_activity);

        SysUtils.getInstance().loadeCoordSet();

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lv_coord;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE

        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        lv_coord = (ListView) findViewById(R.id.lv_coord);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<String, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lv_coord.setAdapter(adapter);
        }
        adapter.refresh(list);
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Entry<String, String>> coordlist;

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                coordlist = getList();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(coordlist);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    protected List<Entry<String, String>> getList() {

        coordlist = new ArrayList<Entry<String, String>>();
        coordlist.add(new Entry<String, String>(getString(R.string.coordset_0), SysUtils.getInstance().getCoordModeStr()));//参考椭球
        coordlist.add(new Entry<String, String>(getString(R.string.coordset_1), SysUtils.getInstance().getCoordFormatStr()));//坐标投影
        return coordlist;
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lv_coord.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onToNext(position);
            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lv_coord).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }


    private void onToNext(int pos) {
        if (pos == 0) {
            //参考椭球 菜单
//            String[] TOPBAR0_NAMES = getResources().getStringArray(R.array.topbar0_names);
//            new ItemDialog(context, TOPBAR0_NAMES, "", DIALOG_SET_TOPBAR0, this).show();
            toActivity(BottomMenuWindow.createIntent(context, SysUtils.COORD_0)
                    .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.coordset_mode0)), REQUEST_TO_BOTTOM_MENU_0, false);
        } else if (pos == 1) {
            //坐标投影 菜单
            if (SysUtils.getInstance().coord_format == 4) {
                //自定义格式
                new ItemDialog(context, getResources().getStringArray(R.array.topbar1_names), "", DIALOG_SET_TOPBAR1, this).show();
            } else if (SysUtils.getInstance().coord_format == 5) {
                //MGRS
                new ItemDialog(context, getResources().getStringArray(R.array.topbar1_names_5), "", DIALOG_SET_TOPBAR1, this).show();
            } else if (SysUtils.getInstance().coord_format == 6) {
                //Lambert
                new ItemDialog(context, getResources().getStringArray(R.array.topbar1_names_6), "", DIALOG_SET_TOPBAR1, this).show();
            } else if (SysUtils.getInstance().coord_format == 7) {
                //Stereograph
                new ItemDialog(context, getResources().getStringArray(R.array.topbar1_names_7), "", DIALOG_SET_TOPBAR1, this).show();
            } else if (SysUtils.getInstance().coord_format == 8) {
                //Polyconic - 多圆锥投影
                new ItemDialog(context, getResources().getStringArray(R.array.topbar1_names_8), "", DIALOG_SET_TOPBAR1, this).show();
            } else if (SysUtils.getInstance().coord_format == 9) {
                //Oblique Mercator
                new ItemDialog(context, getResources().getStringArray(R.array.topbar1_names_9), "", DIALOG_SET_TOPBAR1, this).show();
            } else if (SysUtils.getInstance().coord_format >= 9 && SysUtils.getInstance().coord_format <= 14) {
                toActivity(BottomMenuWindow.createIntent(context, SysUtils.COORD_1)
                                .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.coorset_wnd_0)),
                        REQUEST_TO_BOTTOM_MENU_1, false);
            } else {
                if (SysUtils.getInstance().coord_mode == 0) {
                    toActivity(BottomMenuWindow.createIntent(context, SysUtils.COORD_11)
                                    .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.coorset_wnd_0)),
                            REQUEST_TO_BOTTOM_MENU_1, false);
                } else {
                    toActivity(BottomMenuWindow.createIntent(context, SysUtils.COORD_1)
                                    .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.coorset_wnd_0)),
                            REQUEST_TO_BOTTOM_MENU_1, false);
                }
            }

        }
    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //@user 新增方法

    @Override
    public void finish() {
        SysUtils.getInstance().saveCoordSet();
        TransParaSevenFileHandle.getInstance().save();
        super.finish();
    }

    private final int REQUEST_TO_BOTTOM_MENU_0 = 31;
    private final int REQUEST_TO_BOTTOM_MENU_1 = 32;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        int coord_format = SysUtils.getInstance().coord_format;
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_0:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (selectedPosition >= 0 && selectedPosition < SysUtils.COORD_0.length) {
                        setListValue(0, SysUtils.COORD_0[selectedPosition]);
                        SysUtils.getInstance().coord_mode = selectedPosition;

                        if (selectedPosition == 0 && SysUtils.getInstance().coord_format > 2) {
                            setListValue(1, SysUtils.COORD_1[2]);
                            SysUtils.getInstance().coord_format = 2;
                        }

                        if (selectedPosition > 0) {
                            int i = SysUtils.getInstance().coord_mode;
                            toActivity(CoordParmActivity.createIntent(this, i), 0);
                        }
                    }
                }
                break;
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (selectedPosition >= 0 && selectedPosition < SysUtils.COORD_1.length) {
                        setListValue(1, SysUtils.COORD_1[selectedPosition]);
                        SysUtils.getInstance().coord_format = selectedPosition;
                    }
                }
                break;
        }
    }

    private void setListValue(int index, String newUnit) {
        if (coordlist == null || coordlist.size() != 4) {
            coordlist = getList();
        }

        if (index < 0 || index >= coordlist.size()) {
            return;
        }

        Entry<String, String> kvb = coordlist.get(index);
        kvb.setValue(newUnit);

        adapter.refresh(coordlist);
    }

    private final int DIALOG_SET_TOPBAR0 = 1;
    private final int DIALOG_SET_TOPBAR1 = 2;

    @Override
    public void onDialogItemClick(int requestCode, int position, String item) {
        if (position < 0) {
            position = 0;
        }
        switch (requestCode) {
            case DIALOG_SET_TOPBAR0:
                if (position == 0) {
                    toActivity(BottomMenuWindow.createIntent(context, SysUtils.COORD_0)
                            .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.coordset_mode0)), REQUEST_TO_BOTTOM_MENU_0, false);
                } else {
                    int i = SysUtils.getInstance().coord_mode;
                    toActivity(CoordParmActivity.createIntent(this, i), 0);
                }
                break;
            case DIALOG_SET_TOPBAR1:
                if (position == 0) {
                    //修改坐标投影 显示格式
                    toActivity(BottomMenuWindow.createIntent(context, SysUtils.COORD_1)
                            .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.coordset_mode1)), REQUEST_TO_BOTTOM_MENU_1, false);
                } else {
                    //修改坐标投影 其他参数
                    if (SysUtils.getInstance().coord_format == 4) {
                        CoordParmFileHandle.getInstance().setEdit(0);
                        toActivity(CoordFormatSelfActivity.createIntent(this), 1);
                    } else if (SysUtils.getInstance().coord_format == 5) {
                        toActivity(MGRSParamSetActivity.createIntent(this), 2);
                    } else if (SysUtils.getInstance().coord_format == 6) {
                        if (position == 1) {
                            //Two Standard Par
                            LambertParmFileHandle.getInstance().OneStandPar = false;
                            LambertParmFileHandle.getInstance().mLambertSetInfoEdit = LambertParmFileHandle.getInstance().lambert1;
                        } else if (position == 2) {
                            //One Standard Par
                            LambertParmFileHandle.getInstance().OneStandPar = true;
                            LambertParmFileHandle.getInstance().mLambertSetInfoEdit = LambertParmFileHandle.getInstance().lambert0;
                        }
                        toActivity(LambertSetActivity.createIntent(this), 62);
                    } else if (SysUtils.getInstance().coord_format == 7) {
                        LambertParmFileHandle.getInstance().stereograph = position - 1;
                        if (position == 1) {
                            //Equatorial
                            LambertParmFileHandle.getInstance().mLambertSetInfoEdit = LambertParmFileHandle.getInstance().stereograph0;
                        } else if (position == 2) {
                            //Oblique
                            LambertParmFileHandle.getInstance().mLambertSetInfoEdit = LambertParmFileHandle.getInstance().stereograph1;
                        } else if (position == 3) {
                            //Scale at south pol
                            LambertParmFileHandle.getInstance().mLambertSetInfoEdit = LambertParmFileHandle.getInstance().stereograph2;
                        } else if (position == 4) {
                            //Scale at north pol
                            LambertParmFileHandle.getInstance().mLambertSetInfoEdit = LambertParmFileHandle.getInstance().stereograph3;
                        }
                        toActivity(LambertSetActivity.createIntent(this), 63);
                    } else if (SysUtils.getInstance().coord_format == 8) {
                        //Polyconic - 多圆锥投影
                        LambertParmFileHandle.getInstance().mLambertSetInfoEdit = LambertParmFileHandle.getInstance().polyconic;
                        toActivity(LambertSetActivity.createIntent(this), 64);
                    } else if (SysUtils.getInstance().coord_format == 9) {
                        //Oblique Mercator - 倾斜墨卡托投影
                        if (position == 1) {
                            //Two Points
                            toActivity(TwoPointsSetActivity.createIntent(this, 1), 1);
                        } else if (position == 2) {
                            //Point AZ
                            toActivity(TwoPointsSetActivity.createIntent(this, 0), 1);
                        }
                    } else if (SysUtils.getInstance().coord_format >= 9 && SysUtils.getInstance().coord_format <= 14) {
                        //Philippines zone 1~5   2
                        CoordParmFileHandle.getInstance().setEdit(SysUtils.getInstance().coord_format - 7);
                        toActivity(CoordFormatSelfActivity.createIntent(this), 1);
                    } else {
                        //Britn Grid
                    }
                }
                break;
            default:
                break;
        }
    }

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Adapter
    public class DemoAdapter extends BaseAdapter<Entry<String, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.texttext_view, parent, false);
            }

            TextView tv_name_0 = BaseViewHolder.get(convertView, R.id.tv_name_0);
            TextView tv_name_1 = BaseViewHolder.get(convertView, R.id.tv_name_1);

            final Entry<String, String> kvb = getItem(position);

            tv_name_0.setText(kvb.getKey());
            tv_name_1.setText(kvb.getValue());

            return super.getView(position, convertView, parent);
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }
    }

}
